package com.ibiaoke.customer.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.XcxOpenGroup;
import com.ibiaoke.customer.entity.XcxOpenGroupExample;
import com.ibiaoke.customer.mapper.XcxOpenGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;

@Repository
public class XcxOpenGroupDao extends BaseDao<XcxOpenGroup, XcxOpenGroupExample, Integer, XcxOpenGroupMapper> {

    @Autowired
    private XcxOpenGroupMapper xcxOpenGroupMapper;

    @Override
    public XcxOpenGroupMapper getMapper() {
        return xcxOpenGroupMapper;
    }

    //开团-保存订单
    public XcxOpenGroup saveXcxOpenGroup(XcxOpenGroup xcxOpenGroup){
        insertSelective(xcxOpenGroup);
        return xcxOpenGroup;
    }

    public List<XcxOpenGroup> checkXcxOpenGroup(String OpenCode, Integer goodId, Integer customerId){
        XcxOpenGroupExample example = new XcxOpenGroupExample();
        example.createCriteria().andOpenCodeEqualTo(OpenCode)
                                .andPtGoodsIdEqualTo(goodId)
                                .andUserIdEqualTo(customerId);
        List<XcxOpenGroup> XcxOpenGroupList = xcxOpenGroupMapper.selectByExample(example);
        return XcxOpenGroupList;
    }

    public List<XcxOpenGroup> listXcxOpenGroup(Integer XcxGoodsId,Integer shopId,Page page){
        XcxOpenGroupExample example = new XcxOpenGroupExample();
        example.createCriteria()
                .andPtGoodsIdEqualTo(XcxGoodsId)
                .andIsOpenEqualTo(CommonConsts.YES)
                .andPtCountNotEqualTo(CommonConsts.NO)
                .andIsPayEqualTo(CommonConsts.YES)
                .andRemark1EqualTo(shopId.toString())
                .andOpenTimeLessThanOrEqualTo(new Date())
                .andEndTimeGreaterThanOrEqualTo(new Date());
        page.setTotalCount(xcxOpenGroupMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("end_time desc");
        return xcxOpenGroupMapper.selectByExample(example);
    }

    public XcxOpenGroup getXcxOpenGroupByOpenCodeAndUserIdAndXcxGoodsId(String OpenCode, Customer customer, Integer xcxGoodsId){
        XcxOpenGroupExample example = new XcxOpenGroupExample();
        example.createCriteria().andUserIdEqualTo(customer.getCustomerId())
                                .andPtGoodsIdEqualTo(xcxGoodsId)
                                .andOpenCodeEqualTo(OpenCode);
        List<XcxOpenGroup> XcxOpenGroupList = xcxOpenGroupMapper.selectByExample(example);
        if(XcxOpenGroupList != null && XcxOpenGroupList.size()>0){
            return XcxOpenGroupList.get(0);
        }
        return null;
    }

    public List<XcxOpenGroup> getPtOpenGroupByOpenCodeAndPtGoodsId(String OpenCode, Integer xcxGoodsId) {
        XcxOpenGroupExample example = new XcxOpenGroupExample();
        example.createCriteria().andPtGoodsIdEqualTo(xcxGoodsId)
                                .andOpenCodeEqualTo(OpenCode)
                                .andIsPayEqualTo(1);
        return xcxOpenGroupMapper.selectByExample(example);
    }
    public Integer updateIspayAndCount(Integer id){
        XcxOpenGroup xcxOpenGroup = selectByPrimaryKey(id);
        xcxOpenGroup.setGroupId(id);
        xcxOpenGroup.setIsPay(1);
        return updateByPrimaryKeySelective(xcxOpenGroup);
    }

    public XcxOpenGroup getXcxOpenGroupByOpenCode(String openCode){
        XcxOpenGroupExample example = new XcxOpenGroupExample();
        example.createCriteria().andOpenCodeEqualTo(openCode)
                .andIsOpenEqualTo(1);
        return xcxOpenGroupMapper.selectByExample(example).get(0);
    }

    public int updateXcxOpenGroupCount(XcxOpenGroup xcxOpenGroup,Integer count){
        xcxOpenGroup.setPtCount(count);
        return updateByPrimaryKeySelective(xcxOpenGroup);
    }
    public List<XcxOpenGroup> getXcxOpenGroupByOpenCodeList(String openCode){
        XcxOpenGroupExample example = new XcxOpenGroupExample();
        example.createCriteria().andOpenCodeEqualTo(openCode)
                .andIsOpenEqualTo(1);
        return xcxOpenGroupMapper.selectByExample(example);
    }


}
